package org.erepublik.company;

import org.json.JSONException;
import org.json.JSONObject;

public class CountryData implements JSONAble {

    public ECountry getCountry() {
        return country;
    }
    public void setCountry(ECountry country) {
        this.country = country;
    }
    
    
    
    public double getCurrencyForGold() {
        return currencyForGold;
    }
    public void setCurrencyForGold(double currencyForGold) {
        this.currencyForGold = currencyForGold;
    }
    public double getGoldForCurrency() {
        return goldForCurrency;
    }
    public void setGoldForCurrency(double goldForCurrency) {
        this.goldForCurrency = goldForCurrency;
    }
    public int getPopulation() {
        return population;
    }
    public void setPopulation(int population) {
        this.population = population;
    }
    public double getMinimumSalary() {
        return minimumSalary;
    }
    public void setMinimumSalary(double minimumSalary) {
        this.minimumSalary = minimumSalary;
    }
    public double getGoldTreasury() {
        return goldTreasury;
    }
    public void setGoldTreasury(double goldTreasury) {
        this.goldTreasury = goldTreasury;
    }
    public double getCurrencyTreasury() {
        return currencyTreasury;
    }
    public void setCurrencyTreasury(double currencyTreasury) {
        this.currencyTreasury = currencyTreasury;
    }
    
    
    public double getSpread() {
        return ((currencyForGold-(1.0/goldForCurrency) ) / currencyForGold) * 100.0;
    }
    
    private ECountry country;
    private double currencyForGold;
    private double goldForCurrency;
    private int population;
    private double minimumSalary;
    private double goldTreasury;
    private double currencyTreasury;
    private Location location;
    
    private int newCitizens;
    private int online;
    
    public JSONObject toJSONObject() throws JSONException {
        JSONObject json = new JSONObject();
        if (country==null)
            json.put("country", "null");
        else
            json.put("country", country.toString());
        json.put("currencyForGold", currencyForGold);
        json.put("goldForCurrency", goldForCurrency);      
        json.put("population", population);       
        json.put("minimumSalary", minimumSalary);  
        json.put("goldTreasury", goldTreasury);
        json.put("currencyTreasury", currencyTreasury);
        json.put("newCitizens", newCitizens);
        json.put("online", online);
        return json;
    }
    
    public static CountryData fromJSONObject(JSONObject json) throws JSONException {
        CountryData countryData = new CountryData();
        if (json.get("country") != null && !json.get("country").equals("null")) {
            countryData.setCountry( ECountry.valueOf( json.get("country").toString())  );
        }
        else  {
            countryData.setLocation(EWorld.GLOBAL);
        }
        countryData.setCurrencyForGold( readDoubleFromJSON(json.get("currencyForGold")) );
        countryData.setGoldForCurrency( readDoubleFromJSON(json.get("goldForCurrency"))  );
        countryData.setPopulation( (Integer)json.get("population") );
        countryData.setMinimumSalary( readDoubleFromJSON(json.get("minimumSalary")) );
        countryData.setGoldTreasury( readDoubleFromJSON(json.get("goldTreasury")) );
        countryData.setCurrencyTreasury( readDoubleFromJSON(json.get("currencyTreasury")) );
        if (json.has("online"))
            countryData.setOnline( (Integer)json.get("online") );
        if (json.has("newCitizens"))
            countryData.setNewCitizens( (Integer)json.get("newCitizens") );
        
        return countryData;
    }
    
    
    private static double readDoubleFromJSON(Object object) {
        if (object instanceof Integer)
            return (Integer)object ;
        else
            return (Double)object ;
    }
    
    
    public void setNewCitizens(int newCitizens) {
        this.newCitizens = newCitizens;
    }
    public int getNewCitizens() {
        return newCitizens;
    }
    public void setOnline(int online) {
        this.online = online;
    }
    public int getOnline() {
        return online;
    }
    public void setLocation(Location location) {
        this.location = location;
    }
    public Location getLocation() {
        return location;
    }
    
    
}
